package com.sky.controller.admin;

//import com.sky.annotation.AdminLoginRequired;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
//@AdminLoginRequired
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    // 新增菜品
    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "setmealCache", key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.save(dishDTO);

        // 清理缓存
//        redisTemplate.delete("dish_" + dishDTO.getCategoryId());
//        cleanCache("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }

    //分页查询
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page( DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询菜品：{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    //删除菜品
    @DeleteMapping()
    @ApiOperation("删除菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        dishService.delete(ids);
        // 清理缓存
//        cleanCache("dish_*");
        return Result.success();
    }

    //根据id查询菜品
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishDTO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishDTO dishDTO = dishService.getById(id);
        return Result.success(dishDTO);
    }

    //修改菜品
    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.update(dishDTO);
//        cleanCache("dish_*");
        return Result.success();
    }

    //停售/起售菜品
    @PostMapping("/status/{status}")
    @ApiOperation("停售/起售菜品")
    @CacheEvict(cacheNames = "setmealCache", allEntries = true)
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("停售/起售菜品：{}，{}", status, id);
        dishService.updateStatus(status, id);
//        cleanCache("dish_*");
        return Result.success();
    }


//    public void cleanCache(String categoryId) {
//        log.info("清理菜品缓存：{}", categoryId);
//        Set keys = redisTemplate.keys( categoryId);
//        redisTemplate.delete(keys);
//    }
}
